package com.example.agriculyural.Observer;

import java.util.ArrayList;
import java.util.List;

public class EnvironmentMonitoringCenter implements EnvironmentSubject {
    private List<EnvironmentObserver> observers;
    private EnvironmentData currentData;

    public EnvironmentMonitoringCenter() {
        this.observers = new ArrayList<>();
    }

    @Override
    public void addObserver(EnvironmentObserver observer) {
        observers.add(observer);
        System.out.println("添加观察者: " + observer.getClass().getSimpleName());
    }
    public void setEnvironmentData(EnvironmentData data) {
        this.currentData = data;
        notifyObservers();
    }
    @Override
    public void removeObserver(EnvironmentObserver observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        System.out.println("\n=== 环境数据更新，通知所有农机设备 ===");
        for (EnvironmentObserver observer : observers) {
            observer.update(currentData);
        }
    }
}
